import {getBill} from "../../api/api.js";
import {allMoney} from "../../api/api.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    currentPage:1,
    selTime:"本月",
    allMoney:0,
    actions: [
      {
        name: '选项'
      },
      {
        name: '选项'
      },
      {
        name: '选项',
        subname: '副文本',
        openType: 'share'
      }
    ],
    billList:[]
  },



  /**
   * / openId; "微信用户唯一标识"
  // status; "订单状态", notes = "null/全部,0待付款/1待发货/2待收货/3待评价/4交易成功/5交易关闭/6售后中 "
  // isAbnormal;//是否正常1正常/0异常
  // orderId; //订单号
  // keywords;//查询关键字
  // start;//开始时间
  // end;//结束时间
   * 
   * **/ 
  changeTime(e){
      let time = e.currentTarget.dataset.time;
      this.setData({
          selTime:time,
          currentPage:1
      })

      this.onClose();
      this.getBill(time);
      this.getAllMoney();
      
  },


  getAllMoney(){
     let start = "";
     let end = "";
     if(this.data.selTime == "本月"){
          var day2 = new Date();
          day2.setTime(day2.getTime());
          start = day2.getFullYear()+"-" + (day2.getMonth()+1)+"-01 00:00:00";
          end = day2.getFullYear()+"-" + (day2.getMonth()+1)+"-30 00:00:00";
          console.log(start,end,"时间");
     }else{
          console.log("本年");
          var day2 = new Date();
          day2.setTime(day2.getTime());
          start = day2.getFullYear()+"-" + "01" +"-01 00:00:00";
          end =  day2.getFullYear()+1 +"-" + "01" +"-01 00:00:00";
     }
      
     
     let data ={
        start,
        end,
        openId:wx.getStorageSync('loginInfo').openid
     }
     allMoney(data).then(res=>{
         console.log(res,"获取统计的金额");
         this.setData({
            allMoney:res.data.data
         })
     })
  },


  getBill(time){
        let that = this;

        var start = "";
        var end = "";

        if(time == "本月"){
             //今天的时间
            // 时间这里应该考虑到打小月份
            var day2 = new Date();
            day2.setTime(day2.getTime());
            start = day2.getFullYear()+"-" + (day2.getMonth()+1)+"-01 00:00:00";
            end = day2.getFullYear()+"-" + (day2.getMonth()+1)+"-30 00:00:00";
            console.log(start,end,"时间");
        }else{
            console.log("本年");
            var day2 = new Date();
            day2.setTime(day2.getTime());
            start = day2.getFullYear()+"-" + "01" +"-01 00:00:00";
            end =  day2.getFullYear()+1 +"-" + "01" +"-01 00:00:00";
        }
       

        let data  = {
           currentPage:that.data.currentPage,
           pageSize:10,
           start,
           end,
           openId:wx.getStorageSync('loginInfo').openid
        }

        console.log(data,"发送请求之前的data")


        getBill(data).then(res=>{
            console.log(res,"获取账单")
            // 根据currentpage 判断是第一次加载还是上拉加载
            if(that.data.currentPage==1){
                this.setData({
                  billList:res.data.data.rows,
                  currentPage:++that.data.currentPage
                })
            }else{
              console.log("不是第一页")
              
              // concat 会返回一个新的数组
              let billList = that.data.billList;
              billList =  billList.concat(res.data.data.rows);
              this.setData({
                billList:billList,
                currentPage:++that.data.currentPage
              })
            }
          
        })

        // 
  },

  getMore(){
      console.log("加载更多");

      let selTime = this.data.selTime;
      this.getBill(selTime);
  },



  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.getBill();
      this.getAllMoney();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})